package com.youmu.sichuansfc.controller.fund;

import com.youmu.sichuansfc.ano.FundManager.FundUserAuth;
import com.youmu.sichuansfc.ano.FundManager.FundUserTokenUser;
import com.youmu.sichuansfc.common.CommonRes;
import com.youmu.sichuansfc.domain.YoumuFundManager;
import com.youmu.sichuansfc.domain.YoumuFundManagerFund;
import com.youmu.sichuansfc.domain.YoumuFundUser;
import com.youmu.sichuansfc.domain.pojo.SearchYoumuFundManagerFund;
import com.youmu.sichuansfc.dto.PageT;
import com.youmu.sichuansfc.dto.fund.FundManagerDto;
import com.youmu.sichuansfc.dto.fund.FundManagerFundDto;
import com.youmu.sichuansfc.service.FundManagerService;
import com.youmu.sichuansfc.util.ReadEXCEL;
import org.apache.log4j.Logger;
import org.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 基金管理人 信息
 * @author WYZ
 * @create 2018-01-30 17:55
 * @desc
 **/
@Controller
public class FundManagerController {

    private Logger logger  = Logger.getLogger(this.getClass());
    @Autowired
    FundManagerService fundManagerService;

    /**
     *  基金管理人信息
     * @param youmuFundUser
     * @param fundManagerDto
     * @return
     */
    @RequestMapping(value = "/fund/manager/update.bs", method = RequestMethod.POST)
    @FundUserAuth
    public @ResponseBody CommonRes insertManager(@FundUserTokenUser YoumuFundUser youmuFundUser,
                            @RequestBody FundManagerDto fundManagerDto){
        logger.info(new JSONObject(fundManagerDto));

        logger.info(new JSONObject(youmuFundUser));
        try {
            YoumuFundManager youmuFundManagerChild = null;
            // 根据ID查询是否修改
//            if(fundManagerDto.getId() != null) {
////                int updateCount = fundManagerService.checkFundUserManager(youmuFundUser.getId(), fundManagerDto.getId()).getUpdateCount();
////                if (updateCount >= 3){
////                    return  CommonRes.checkFail("修改次数超限");
////                }
////                fundManagerDto.setUpdateCount(updateCount + 1);
//                youmuFundManagerChild = fundManagerService.checkFundUserManager(youmuFundUser.getId(), fundManagerDto.getId());
//                if( null == youmuFundManagerChild)
//                    return CommonRes.checkFail("ID 不存在");
//            }
             // 根据名称查询是否已经存在
            if(fundManagerService.findByManagerName(fundManagerDto.getManagerName()) != null){
                return  CommonRes.checkFail("你提交的管理人已经存在");
            }
            // 上传基金时 查询是否已经有管理人 一个账号只能有一个
            List<FundManagerDto> fundManagerDtos = fundManagerService.findByFundUserId(youmuFundUser.getId());
            if (fundManagerDtos != null && fundManagerDtos.size() > 0){
                return  CommonRes.checkFail("一个账号只能有一个管理人");
            }
            YoumuFundManager youmuFundManager = fundManagerService.uploadFundManager(fundManagerDto, youmuFundUser.getId());

            return  CommonRes.success(youmuFundManager.getId());
        }catch (Exception e){
            e.printStackTrace();
        }
        return  CommonRes.fail();
    }

    /**
     *  基金管理人 基金信息报送
     * @param youmuFundUser
     * @return
     */
    @RequestMapping(value = "/fund/manager/fund/update.bs", method = RequestMethod.POST)
    @FundUserAuth
    public @ResponseBody  CommonRes insertFund(@FundUserTokenUser YoumuFundUser youmuFundUser,
                                 @RequestBody FundManagerFundDto fundManagerFundDto,
                                               HttpServletResponse httpServletResponse){

        logger.info(new JSONObject(fundManagerFundDto));

        // 查询 上传 基金管理人ID是否在改账号下
        try {
            YoumuFundManager youmuFundManager = fundManagerService.checkFundUserManager(youmuFundUser.getId(), fundManagerFundDto.getFundManagerId());
            if (youmuFundManager == null){
                return  CommonRes.checkFail("非法操作");
            }

//            //  修改该基金时 先查询改基金信息
//            if(fundManagerFundDto.getId() != null){
//                int updateCount = fundManagerService.findFund(youmuFundUser, fundManagerFundDto.getId()).getUpdateCount();
//                if (updateCount >= 3){
//                    return  CommonRes.checkFail("修改次数超限");
//                }
//                fundManagerFundDto.setUpdateCount(updateCount + 1);
//            }
            logger.info(new JSONObject(fundManagerFundDto));

            logger.info("manager id : " + youmuFundManager.getId());
            YoumuFundManagerFund youmuFundManagerFund = fundManagerService.uploadFund(youmuFundUser.getId(), fundManagerFundDto);
            return  CommonRes.success(youmuFundManagerFund);
        }catch (Exception e){
            e.printStackTrace();
        }

        return  CommonRes.fail();
    }

    /**
     *  查询改账户下基金管理人信息
     * @param youmuFundUser
     * @return
     */
    @RequestMapping(value = "/fund/manager/detail.bs", method = RequestMethod.POST)
    @FundUserAuth
    public @ResponseBody  CommonRes fundManager(@FundUserTokenUser YoumuFundUser youmuFundUser,
                                    @RequestParam(name = "id") Long fundManagerId){

        try {
            FundManagerDto fundManagerDto =  fundManagerService.findById(youmuFundUser.getId(), fundManagerId);
            logger.info(new JSONObject(fundManagerDto));
            System.out.println(new JSONObject(fundManagerDto));
            return CommonRes.success(fundManagerDto);
        }catch (Exception e){
            e.printStackTrace();
        }
        return CommonRes.fail();
    }


    /**
     *  查询单个基金详情
     * @param youmuFundUser
     * @param fundManagerFundId
     * @return
     */
    @RequestMapping(value = "/fund/manager/fund/detail.bs", method = RequestMethod.POST)
    @FundUserAuth
    public @ResponseBody  CommonRes fundManagerFund(@FundUserTokenUser YoumuFundUser youmuFundUser,
                                     @RequestParam(name = "id") Long fundManagerFundId){

        try {
            YoumuFundManagerFund youmuFundManagerFund = fundManagerService.findFund(youmuFundUser, fundManagerFundId);
            return  CommonRes.success(youmuFundManagerFund);
        }catch (Exception e){
            e.printStackTrace();
        }
        return  CommonRes.fail();
    }


    /**
     *  后台管理人员拉取 私募基金管理人列表
     * @param investType 投资类型 1 私募证券投资基金管理人；2 私募股权、创业投资基金管理人； 3 其他私募投资基金管理人
     * @param fundManagerName  私募管理人名
     * @param num
     * @param size
     * @return
     */
    @RequestMapping(value = "/fund/manager", method = RequestMethod.GET)
    public @ResponseBody CommonRes fundManagers(@RequestParam(name = "investType", required = false)  Integer investType,
                           @RequestParam(name = "name", required = false)  String fundManagerName,
                           @RequestParam(name = "num", required = false, defaultValue = "0") int  num,
                           @RequestParam(name = "size", required = false, defaultValue = "10") int size){
        try {
            logger.info("req fundManagerName : " + fundManagerName);
            PageT pageT = fundManagerService.funManagers(investType, fundManagerName, num, size);
            return  CommonRes.success(pageT);
        }catch (Exception e){
            e.printStackTrace();
        }

        return CommonRes.fail();
    }

    /**
     *  基金管理人Excel 导出
     * @param investType 投资类型 1 私募证券投资基金管理人；2 私募股权、创业投资基金管理人； 3 其他私募投资基金管理人
     * @param fundManagerName  私募管理人名
     * @return
     */
    @RequestMapping(value = "/fund/manager/excel", method = RequestMethod.GET)
    public @ResponseBody CommonRes fundManagersExcel(
                            HttpServletRequest request,
                            HttpServletResponse response,
                            @RequestParam(name = "investType", required = false)  Integer investType,
                            @RequestParam(name = "name", required = false)  String fundManagerName){
        try {
            PageT pageT = fundManagerService.funManagers(investType, fundManagerName, 0, 10000);
            String excelPath = new ReadEXCEL().exportManagerExcel(request,response,pageT.getPageList());
            return  null;
        }catch (Exception e){
            e.printStackTrace();
        }

        return CommonRes.fail();
    }

    /**
     *  后台管理人员查看某个基金管理人员详情
     * @param fundManagerId
     * @return
     */
    @RequestMapping(value = "/fund/manager/detail", method = RequestMethod.GET)
    public @ResponseBody CommonRes fundManager(@RequestParam(name = "id")  Long fundManagerId){
        try {
            FundManagerDto  fundManagerDto = fundManagerService.findmanagerById(fundManagerId);
            return  CommonRes.success(fundManagerDto);
        }catch (Exception e){
            e.printStackTrace();
        }
        return  CommonRes.fail();
    }


    /**
     * ；拉取所有基金列表
     * @param fundType 基金类型：1私募证券投资基金；2 私募股权、创业投资基金；3其他投资基金
     * @param searchName  私募基金管理人名称/ 私募基金名称
     * @param num
     * @param size
     * @return
     */
    @RequestMapping(value = "/fund/manager/fund", method = RequestMethod.GET)
    public @ResponseBody CommonRes funds(@RequestParam(name = "fundType", required = false)  Integer fundType,
                                  @RequestParam(name = "name", required = false)  String searchName,
                                  @RequestParam(name = "num", required = false, defaultValue = "0") int  num,
                                  @RequestParam(name = "size", required = false, defaultValue = "10") int size){
        try {
            PageT pageT = fundManagerService.searchFund(fundType, searchName, num, size);
            return  CommonRes.success(pageT);
        }catch (Exception e){
            e.printStackTrace();
        }

        return CommonRes.fail();
    }

    /**
     * 所有基金列表Excel 导出
     * @param fundType 基金类型：1私募证券投资基金；2 私募股权、创业投资基金；3其他投资基金
     * @param searchName  私募基金管理人名称/ 私募基金名称
     * @return
     */
    @RequestMapping(value = "/fund/manager/fund/excel", method = RequestMethod.GET)
    public @ResponseBody CommonRes fundsExcel(@RequestParam(name = "fundType", required = false)  Integer fundType,
                                         @RequestParam(name = "name", required = false)  String searchName,
                                              HttpServletResponse httpServletResponse,
                                              HttpServletRequest httpServletRequest){
        try {
            PageT pageT = fundManagerService.searchFund(fundType, searchName, 0, 1000000);
            String excelPath = new ReadEXCEL().exportManagerFundExcel(httpServletRequest,httpServletResponse,pageT.getPageList());
            return  null;
        }catch (Exception e){
            e.printStackTrace();
        }

        return CommonRes.fail();
    }
    /**
     * 查询单个基金详情
     * @param fundId
     * @return
     */
    @RequestMapping(value = "/fund/manager/fund/detail", method = RequestMethod.GET)
    public @ResponseBody CommonRes fund(@RequestParam(name = "id") Long fundId){
        try {
            SearchYoumuFundManagerFund searchYoumuFundManagerFund = fundManagerService.findFundById(fundId);
            return CommonRes.success(searchYoumuFundManagerFund);
        }catch (Exception e){
            e.printStackTrace();
        }
        return  CommonRes.fail();
    }
//
//    /**
//     * 拉取账号下管理人列表,和基金列表
//     * @param youmuFundUser
//     * @return
//     */
//    @RequestMapping(value = "/fund/manager/profile.bs", method = RequestMethod.GET)
//    @FundUserAuth
//    public @ResponseBody CommonRes fundManagerList(@FundUserTokenUser YoumuFundUser youmuFundUser){
//        List<Map<String, Object>> mapManger = new ArrayList<>();
//
//        try {
//            List<FundManagerDto> fundManagerDtos = fundManagerService.findByFundUserId(youmuFundUser.getId());
//            if (fundManagerDtos != null && fundManagerDtos.size() > 0){
//                for (FundManagerDto fundManagerDto : fundManagerDtos){
//                    Map<String, Object> map = new HashMap<>();
//                    map.put("managerName",fundManagerDto.getManagerName());
//                    map.put("id",fundManagerDto.getId());
//                    List<Map<String, Object>> mapFundList = new ArrayList<>();
//
//                    List<YoumuFundManagerFund> youmuFundManagerFundList = fundManagerService.findByManagerId(fundManagerDto.getId());
//                    if (youmuFundManagerFundList != null && youmuFundManagerFundList.size() > 0){
//                        for (YoumuFundManagerFund youmuFundManagerFund : youmuFundManagerFundList){
//                            Map<String , Object> map1 = new HashMap<>();
//                            map1.put("fundName", youmuFundManagerFund.getFundName());
//                            map1.put("id", youmuFundManagerFund.getId());
//                            mapFundList.add(map1);
//                        }
//                    }
//                    map.put("funds",mapFundList);
//                    mapManger.add(map);
//                }
//            }
//            return  CommonRes.success(mapManger);
//        }catch (Exception e){
//            e.printStackTrace();
//        }
//        return  CommonRes.fail();
//    }

}
